package fr.babos.test.virtual.elements 
{
	import com.greensock.easing.Strong;
	import com.greensock.TweenMax;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.text.GridFitType;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import fr.babos.graphic.components.base.BaseContainer;
	import fr.babos.graphic.components.base.ResizableComponent;
	import fr.babos.graphic.medias.ImageLoader;
	import fr.babos.test.virtual.items.TweetItem;
	import fr.babos.touch.delegate.IGestureTouchDelegate;
	import fr.babos.touch.emulator.GestureTouchEmulator;
	import fr.babos.touch.events.GestureTouchDragEvent;
	import fr.babos.touch.events.GestureTouchEvent;
	
	/**
	 * @author ZoulouX
	 */
	public class TweetListElement extends ResizableComponent implements IGestureTouchDelegate
	{
		protected var _bitmap			:Bitmap;
		protected var _bitmapData		:BitmapData;
		
		protected var _item				:TweetItem;
		
		protected var _image			:ImageLoader;
		protected var _textField		:TextField;
		
		public function TweetListElement (pItem:TweetItem = null)
		{
			_item = pItem;
			
			mouseChildren = false;
		}
		
		protected function drawBack ():void
		{
			if (_bitmapData != null)
				_bitmapData.dispose();
			
			_bitmapData = new BitmapData(_localWidth, _localHeight + 1, false, 0xFFFFFF);
			_bitmapData.setPixel(0, height - 2, 0xDDDDDD);
			_bitmapData.setPixel(0, height - 1, 0xBBBBBB);
			
			if (_bitmap == null)
				_bitmap = new Bitmap(_bitmapData, PixelSnapping.ALWAYS, false);
			else
				_bitmap.bitmapData = _bitmapData;
			
			_back.width = _localWidth;
			_back.height = _localHeight;
		}
		
		protected function drawImage ():void
		{
			if (_image != null && _image.bitmapData != null)
			{
				
			}
		}
		
		protected function drawText ():void
		{
			
		}
		
		
		override public function init ():void
		{
			drawBack();
			
			if (_item != null)
			{
				_image = new ImageLoader(height - 20, height - 20, "", "stretch");
				
				_image.tweenOnComplete = false;
				_image.smooth = false;
				_image.load(_item.avatar);
				
				_image.addEventListener(Event.COMPLETE, imageLoadedHandler);
				
				//_image.x = 10;
				//_image.y = 10;
				
				//addChild(image);
				
				var textContainer:BitmapTextField = new BitmapTextField();
				_textField = textContainer.$value;
				
				_textField.x = height;
				_textField.y = 7;
				
				_textField.multiline = true;
				_textField.wordWrap = true;
				_textField.height = height - 7;
				
				//_textField.gridFitType = GridFitType.NONE;
				
				_textField.selectable = false;
				_textField.embedFonts = true;
				
				//_textField.htmlText = '<font color="#FFFFFF">' + _item.text + '</font><br><font color="#CCCCCC" size="12px">' + _item.screenName + '</font>';
				_textField.htmlText = '<font color="#000000" size="30px">' + _item.text + '</font><br><font color="#777777" size="30px">' + _item.screenName + '</font>';
				
				//textContainer.cacheAsBitmap = true;
				//textContainer.cacheAsBitmapMatrix = new Matrix();
				
				//addChild(textContainer);
				
				_bitmapData.draw();
				
				//GestureTouchEmulator.emulate(this);
			}
		}
		
		protected function imageLoadedHandler (event:Event):void
		{
			trace("loaded");
		}
		
		public function gestureTouchHandler (event:GestureTouchEvent):void
		{
			if (event is GestureTouchDragEvent)
			{
				var dragEvent:GestureTouchDragEvent = event as GestureTouchDragEvent;
				
				if (dragEvent.isHorizontal && dragEvent.type == GestureTouchDragEvent.SINGLE_DRAGGING)
				{
					x += dragEvent.xDelta;
				}
				else if (dragEvent.isHorizontal && dragEvent.type == GestureTouchDragEvent.STOP_DRAG)
				{
					if ((x > 0 && dragEvent.xDelta > 1) || (x < 0 && dragEvent.xDelta < 1))
					{
						const destinationX:int = (dragEvent.xDelta > 0 ? 1 : -1) * width;
						TweenMax.killTweensOf(this, false);
						TweenMax.to(this, .4, {
							x: destinationX,
							ease: Strong.easeOut,
							onComplete: function (pThis:TweetListElement):void
							{
								pThis.visible = false;
								TweenMax.to(pThis, .4, {
									height: 0,
									x: destinationX * 2,
									ease: Strong.easeInOut,
									onComplete: _item.remove,
									onCompleteParams: [pThis]
								});
							},
							onCompleteParams: [this]
						});
					}
					else
					{
						TweenMax.to(this, .5, {
							x: 0,
							ease: Strong.easeOut
						});
					}
				}
			}
		}
		
		override protected function needReplace (pNeedResized:Boolean = false):void
		{
			super.needReplace(pNeedResized);
			
			if (_back != null && width > 0)
			{
				_back.width = width;
			}
			
			if (_textField != null && width > 0)
			{
				_textField.width = width - height - 10;
			}
		}
		
		override public function dispose ():void
		{
			if (_image != null)
				_image.removeEventListener(Event.COMPLETE, imageLoadedHandler);
			
			//GestureTouchEmulator.demulate(this);
		}
	}
}